package at.fhj.itm.view;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Screen;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class TranslationBox extends MIDlet implements CommandListener{

	private Display display;
	private Command startC,exitC,helpC,aboutUsC,bookmarksC,backC;
	private Form menuForm,userInputForm,suggesForm,resultForm;
	private TextField getData, searchData;
	private List mainScr,nameScr;
	private String[] mainScrChoices = {"START","HELP","ABOUT US"};
	private Form entryScr;
	
	private Form optionScr;
	private Form searchScr;
	private ChoiceGroup sortChoice;
	 
	
	
	public TranslationBox() {
		
		menuForm      = new Form("DerBegleiter");
		resultForm    = new Form("Uebersetzung");
		userInputForm = new Form("User Input"  );
		
		getData = new TextField("Gib ein Wort ein", null, 30, TextField.PLAIN);
		
		startC   = new Command("Select", Command.OK,   1);
		exitC    = new Command("Exit",  Command.EXIT, 1);
		backC    = new Command("Back",  Command.BACK, 1);
		helpC    = new Command("Help",  Command.OK,1);
		aboutUsC = new Command("About Us", Command.OK,1);
		
		userInputForm.append(getData);
		
	}

	

	protected void destroyApp(boolean unconditional)
			throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
		
	}

	protected void startApp() throws MIDletStateChangeException {
		
		display = Display.getDisplay(this);
		
		menuForm.addCommand(aboutUsC);
		menuForm.addCommand(exitC);
		menuForm.addCommand(helpC);
		menuForm.addCommand(startC);
		menuForm.setCommandListener(this);
		//display.setCurrent(menuForm);
		
		genSearchScr();
		genEntryScr();
		genMainScr();
	
		
		
	}
	/**
	 * Create the first screen of our MIDLet.
	 * This screen is a list.
	 */
	private Screen genMainScr(){
		
		if(mainScr == null)
		{
			mainScr = new List("Menu", List.IMPLICIT,mainScrChoices,null);
			mainScr.addCommand(exitC);
			mainScr.addCommand(startC);
			mainScr.setCommandListener(this);
		}
		display.setCurrent(mainScr);
		return mainScr;
	}
	/**
	 * Display Textfield for the user entry 
	 * This use to capture data from user and then it will be translated   
	 */
	private Screen genEntryScr(){
		
		if(entryScr == null)
		{
			entryScr = new Form("New Word");
			entryScr.addCommand(startC);
			entryScr.addCommand(exitC);
			entryScr.setCommandListener(this);
			
			getData = new TextField("Gib ein Wort ein", null, 30, TextField.PLAIN);
		}
		
		getData.delete(0, getData.size());
		
		
		display.setCurrent(entryScr);
		return entryScr;
	}
	/**
	 * Search screen
	 * 
	 * Displays one textfield, this used for searching the dictionary 
	 */
	private Screen genSearchScr(){
		
		if(searchScr == null)
		{
			searchScr = new Form("Search");
			searchScr.addCommand(backC);
			searchScr.addCommand(startC);
			searchScr.setCommandListener(this);
			
			searchData = new TextField("Gib ein Wort ein", "", 30, TextField.ANY);
			
		}
		searchData.delete(0, searchData.size());
		
		display.setCurrent(searchScr);
		return searchScr;
		
	}
public void commandAction(Command c, Displayable d) {
		
		if(c ==exitC)
		{
			try {
				destroyApp(true);
			} catch (MIDletStateChangeException e) {
			
				e.printStackTrace();
			}
			notifyDestroyed();
		}
		
	}

}
